package com.joey.color_test;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.holly.common_view.ToViewUtils;
import com.holly.common_view.adapter.BaseAdapter;
import com.holly.common_view.adapter.BaseHolder;
import com.holly.common_view.listener.OnItemClickListener;

public class MainActivity extends AppCompatActivity {
    private Button mBtStroke;
    private Button mBtText;
    private Button mBtSolid;
    private RecyclerView mRecyclerView;
    private ColorChangeDialog mColorChangeDialog;
    private Adapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mBtStroke = (Button) findViewById(R.id.bt_stroke);
        mBtText = (Button) findViewById(R.id.bt_text);
        mBtSolid = (Button) findViewById(R.id.bt_solid);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv);

        int minHeight = getResources().getDimensionPixelSize(R.dimen.y40);
        int minWidth = getResources().getDimensionPixelSize(R.dimen.y100);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new Adapter();
        mAdapter.setMinHeight(minHeight);
        mAdapter.setMinWidth(minWidth);
        mAdapter.setTextColor(Color.GRAY);
        mAdapter.setStrokeColor(Color.GRAY);
        mAdapter.setSolidColor(Color.WHITE);

        mRecyclerView.setAdapter(mAdapter);

        mColorChangeDialog = new ColorChangeDialog(this);
        mBtText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mColorChangeDialog.isShowing()) {
                    mColorChangeDialog.dismiss();
                }
                mColorChangeDialog.setOnColorChangeListener(new OnItemClickListener<Integer>() {
                    @Override
                    public void onItemClick(Integer item) {
                        mAdapter.setTextColor(item);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                mColorChangeDialog.setColor(mAdapter.getTextColor());
                mColorChangeDialog.show();
            }
        });

        mBtSolid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mColorChangeDialog.isShowing()) {
                    mColorChangeDialog.dismiss();
                }

                mColorChangeDialog.setOnColorChangeListener(new OnItemClickListener<Integer>() {
                    @Override
                    public void onItemClick(Integer item) {
                        mAdapter.setSolidColor(item);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                mColorChangeDialog.setColor(mAdapter.getSolidColor());
                mColorChangeDialog.show();
            }
        });

        mBtStroke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mColorChangeDialog.isShowing()) {
                    mColorChangeDialog.dismiss();
                }

                mColorChangeDialog.setOnColorChangeListener(new OnItemClickListener<Integer>() {
                    @Override
                    public void onItemClick(Integer item) {
                        mAdapter.setStrokeColor(item);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                mColorChangeDialog.setColor(mAdapter.getStrokeColor());
                mColorChangeDialog.show();
            }
        });
    }

    private class Adapter extends BaseAdapter {
        private int mTextColor;
        private int mStrokeColor;
        private int mSolidColor;

        private int mMinHeight;
        private int mMinWidth;

        private int getTextColor() {
            return mTextColor;
        }

        private void setTextColor(int textColor) {
            this.mTextColor = textColor;
        }

        private int getStrokeColor() {
            return mStrokeColor;
        }

        private void setStrokeColor(int strokeColor) {
            this.mStrokeColor = strokeColor;
        }

        private int getSolidColor() {
            return mSolidColor;
        }

        private void setSolidColor(int solidColor) {
            this.mSolidColor = solidColor;
        }

        private void setMinHeight(int minHeight) {
            this.mMinHeight = minHeight;
        }

        private void setMinWidth(int minWidth) {
            this.mMinWidth = minWidth;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(ToViewUtils.toView(parent, R.layout.item_color_text));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            Holder vh = (Holder) holder;

            TextView textView = vh.mTextView;
            ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
            layoutParams.height = (int) (mMinHeight + mMinHeight * (position + 1) * 0.5f);
            layoutParams.width = (int) (mMinWidth + mMinWidth * (position + 1) * 0.5f);

            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(mSolidColor);
            drawable.setStroke(2, mStrokeColor);
            textView.setBackground(drawable);
            textView.setTextColor(mTextColor);
        }

        @Override
        public int getItemCount() {
            return 12;
        }

        private class Holder extends BaseHolder {
            private final TextView mTextView;

            private Holder(View itemView) {
                super(itemView);
                mTextView = (TextView) itemView.findViewById(R.id.tv);
            }
        }
    }
}
